﻿
// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.BannerName = ko.observable(data.BannerTransactionInfo.BannerName).extend({ maxLength: 250 }).extend({ required: { message: "Tên quảng bá là bắt buộc"} });
    
    this.BannerOptionId = ko.observable(data.BannerTransactionInfo.BannerOptionID);
    this.BannerOptionName = ko.observable(data.BannerTransactionInfo.BannerOptionName);
    this.BannerOptions = ko.observableArray(data.ListBannerOption);

    this.BannerGroupId = ko.observable(data.BannerTransactionInfo.BannerGroupID);
    this.BannerGroupName = ko.observable(data.BannerTransactionInfo.BannerGroupName);
    this.BannerGroups = ko.observableArray(data.ListBannerGroup);

    this.BannerPositionId = ko.observable(data.BannerTransactionInfo.BannerPositionID);
    this.BannerPositionName = ko.observable(data.BannerTransactionInfo.BannerPositionName);
    this.BannerPositions = ko.observableArray(data.ListBannerPosition);

    this.PaymentId = ko.observable(data.BannerTransactionInfo.PaymentID);
    this.PaymentName = ko.observable(data.BannerTransactionInfo.PaymentName);
    this.Payments = ko.observableArray(data.ListPaymentMethod);

    this.StartDate = ko.observable(data.BannerTransactionInfo.StartDate);
    this.EndDate = ko.observable(data.BannerTransactionInfo.EndDate);
    this.ListPrice = ko.observable(data.BannerTransactionInfo.ListPrice);
    this.UnitPrice = ko.observable(data.BannerTransactionInfo.UnitPrice);
    this.DateCreated = ko.observable(data.BannerTransactionInfo.DateCreated);
    this.TransactionDate = ko.observable(data.BannerTransactionInfo.TransactionDate);

    this.Fullname = ko.observable(data.BannerTransactionInfo.Billing_Fullname);
    this.Address1 = ko.observable(data.BannerTransactionInfo.Billing_Address1);
    this.Address2 = ko.observable(data.BannerTransactionInfo.Billing_Address2);
    this.City = ko.observable(data.BannerTransactionInfo.Billing_City);
    this.State = ko.observable(data.BannerTransactionInfo.Billing_State);
    this.Country = ko.observable(data.BannerTransactionInfo.Billing_Country);
    this.Zipcode = ko.observable(data.BannerTransactionInfo.Billing_ZipCode);
    this.Phone = ko.observable(data.BannerTransactionInfo.Billing_Phone);
    this.Fax = ko.observable(data.BannerTransactionInfo.Billing_Fax);
    this.Approved = ko.observable(data.BannerTransactionInfo.Approved);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));

$(document).ready(function () {
    $(".calendar-date").datepicker();
});